package com.fancon.android.gardenjokesx.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.fancon.android.gardenjokesx.GardenJokesBaseActivity;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.entity.Notify;
import com.fancon.android.gardenjokesx.entity.User;
import com.fancon.android.gardenjokesx.rpc.GardenJokesApiConnect;
import com.fancon.android.gardenjokesx.rpc.JsonPaser;
import com.fancon.android.gardenjokesx.social.DatabaseHelper;
import com.fancon.android.gardenjokesx.utils.Constant;
import com.fancon.android.gardenjokesx.utils.UserUtil;
import com.fancon.android.multithread.RequestUI;
import com.fancon.android.ui.widget.FanconProgressBar;

public class NotificationSettingActivity extends GardenJokesBaseActivity implements OnClickListener{
	
	private Button mBtnPhotoAllUser;
	private Button mBtnPhotoFollowUser;
	private Button mBtnPhotoOff;
	private Button mBtnCommentAllUser;
	private Button mBtnCommentFollowUser;
	private Button mBtnCommentOff;
	private Button mBtnContactAllUser;
	private Button mBtnContactOff;
	private Button mBtnTitleLosePush;
	private Button mBtnTitleLoseOff;
	
	private Button mBtnDone;
	private String ALL = "0";
	private String FOLLOW = "1";
	private String OFF = "2";
	private String mNofButton;
	private String mNofComment;
	private String mNofContact;
	
	private Notify notify; 
	private Boolean isFirst = true;
	DatabaseHelper db;
	User user = new User();
	String userId;
	Boolean canTap = true;
	
	 @Override  
	    public boolean onCreateOptionsMenu(Menu menu) {  
	      return false;
	    }  
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.u05);
		user = UserUtil.getInfoUserLogin(this);
		userId = user.getUser_id();
		mBtnPhotoAllUser = (Button)findViewById(R.id.u05_btnPhotoAllUser);
		mBtnPhotoAllUser.setOnClickListener(this);
		mBtnPhotoFollowUser = (Button)findViewById(R.id.u05_btnPhotoFollowUser);
		mBtnPhotoFollowUser.setOnClickListener(this);
		mBtnPhotoOff = (Button)findViewById(R.id.u05_btnPhotoOff);
		mBtnPhotoOff.setOnClickListener(this);
		mBtnCommentAllUser = (Button)findViewById(R.id.u05_btnCommentAllUser);
		mBtnCommentAllUser.setOnClickListener(this);
		mBtnCommentFollowUser = (Button)findViewById(R.id.u05_btnCommentFollowUser);
		mBtnCommentFollowUser.setOnClickListener(this);
		mBtnCommentOff = (Button)findViewById(R.id.u05_btnCommentOff);
		mBtnCommentOff.setOnClickListener(this);
		mBtnContactAllUser = (Button)findViewById(R.id.u05_btnContactAllUser);
		mBtnContactAllUser.setOnClickListener(this);
		mBtnContactOff = (Button)findViewById(R.id.u05_btnContactOff);
		mBtnContactOff.setOnClickListener(this);
		mBtnDone = (Button)findViewById(R.id.u05_btnDone);
		mBtnDone.setOnClickListener(this);
		mBtnDone.setClickable(false);
		
		NofSettingRequest request = new NofSettingRequest("u05Activity", NotificationSettingActivity.this);
		getGlobalState().getRequestQueue().addRequest(request);
	}
	
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (canTap) {
			
			if (v == mBtnPhotoAllUser) {
				setNofButton(ALL);
			}
			if (v == mBtnPhotoFollowUser) {
				setNofButton(FOLLOW);
			}
			if (v == mBtnPhotoOff) {
				setNofButton(OFF);
			}
			
			if (v == mBtnCommentAllUser) {
				setNofComment(ALL);
			}
			if (v == mBtnCommentFollowUser) {
				setNofComment(FOLLOW);
			}
			if (v == mBtnCommentOff) {
				setNofComment(OFF);
			}
			
			if (v == mBtnContactAllUser) {
				setNofContact(Constant.PUBLIC);
			}
			if (v == mBtnContactOff) {
				setNofContact(Constant.NOT_PUBLIC);
			}
			
			if (v == mBtnDone) {
				
				//Register Setting to server and move to U02
				RegisterNofSettingRequest request = new RegisterNofSettingRequest("u05Activity", NotificationSettingActivity.this);
				getGlobalState().getRequestQueue().addRequest(request);
			}
		}
		
	}
	
	/**
	 * Set button with appropriate background and text.
	 * @param btn: Button.
	 * @param state: Boolean( ON-true, OFF-false). State to set to button.
	 */
	public void setButtonState(Button btn, String state){
		Drawable d;
		if (state != null) {
			if( state.equals(Constant.PUBLIC)){
				d = getResources().getDrawable(R.drawable.common_btn_bg_on);
				btn.setBackgroundDrawable(d);
				btn.setText(getString(R.string.common_On));
			}
			else {
				d = getResources().getDrawable(R.drawable.common_btn_bg_off);
				btn.setBackgroundDrawable(d);
				btn.setText(getString(R.string.common_Off));
			}
		}
	}
	
	/**
	 * Get notification from server
	 * 
	 * @author DUYTX
	 *
	 */
	class NofSettingRequest extends RequestUI {
		private Activity context;
//		private ProgressDialog mProgressDialog;
		private FanconProgressBar progress;
		public NofSettingRequest(Object key, Activity activity) {
			super(key, activity);
			context = activity;
			if(isFirst){
				progress = FanconProgressBar.show(context, context.getString(R.string.common_loading));
			}
			// TODO Auto-generated constructor stub
		}
		

		@Override
		public void execute()  {
			// TODO Auto-generated method stub
			
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
			nameValuePairs.add(new BasicNameValuePair("user_id",
					userId));
			GardenJokesApiConnect api = new GardenJokesApiConnect(context);
			String result = api.execGet(context, GardenJokesApiConnect.GROUP_U, "getNotification",
					nameValuePairs);
			Log.v("Nof", result);
			JsonPaser jP = new JsonPaser();
			notify = new Notify();
			notify = null;
			if (result != null && result.length() > 0) {
				notify = jP.getNotification(result);
			} else {
				canTap = false;
			}
				
				
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			if (notify != null) {
				mNofButton = String.valueOf(notify.getNof_button());
				setNofButton(mNofButton);
				mNofComment = String.valueOf(notify.getNof_comment());
				setNofComment(mNofComment);
				mNofContact = String.valueOf(notify.getNof_contact());
				setNofContact(mNofContact);
				mBtnDone.setClickable(true);
				
			} else {
				mBtnDone.setClickable(false);
			}
			if(progress!=null){
				progress.dismiss();
			}
			isFirst = false;
			
			
		}
		
	}
	
	
	/**
	 * Get notification from server
	 * 
	 * @author DUYTX
	 *
	 */
	class RegisterNofSettingRequest extends RequestUI {
		private Activity context;
		private FanconProgressBar progress;
		public RegisterNofSettingRequest(Object key, Activity activity) {
			super(key, activity);
			context = activity;
			if(isFirst){
				progress = FanconProgressBar.show(context, context.getString(R.string.common_loading));
			}
			// TODO Auto-generated constructor stub
		}
		

		@Override
		public void execute()  {
			// TODO Auto-generated method stub
			
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(10);
			nameValuePairs.add(new BasicNameValuePair("user_id",
					userId));
			nameValuePairs.add(new BasicNameValuePair("nof_button",
					mNofButton));
			nameValuePairs.add(new BasicNameValuePair("nof_comment",
					mNofComment));
			nameValuePairs.add(new BasicNameValuePair("nof_contact",
					mNofContact));
			nameValuePairs.add(new BasicNameValuePair("face_title",
					"1"));
			nameValuePairs.add(new BasicNameValuePair("twitter_title",
					"1"));
			nameValuePairs.add(new BasicNameValuePair("face_pennant",
					"1"));
			nameValuePairs.add(new BasicNameValuePair("twitter_pennant",
					"1"));
			nameValuePairs.add(new BasicNameValuePair("lost_title",
					"1"));
			SharedPreferences settings = getSharedPreferences(Constant.PREFS_REGISTER, 0);
			String registerID = settings.getString(Constant.REGISTRATION_ID, "");
			nameValuePairs.add(new BasicNameValuePair("device_token",
					registerID));
			GardenJokesApiConnect api = new GardenJokesApiConnect(context);
			String result = api.execPost(getApplicationContext(), GardenJokesApiConnect.GROUP_U, "registerNotification",
					nameValuePairs);
			Log.v("RegisterNof", result);
			
				
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			
			if(progress!=null){
				progress.dismiss();
			}
			isFirst = false;
			context.finish();
			
		}
		
	}
	/**
	 * Set state of Button 
	 * 
	 * @param nofButton
	 */
	public void setNofButton(String nofButton) {
		mBtnPhotoAllUser.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_not_choose, 0, 0, 0);
		mBtnPhotoFollowUser.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_not_choose, 0, 0, 0);
		mBtnPhotoOff.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_not_choose, 0, 0, 0);
		if (nofButton.equals(ALL)) {
			mBtnPhotoAllUser.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_choose, 0, 0, 0);
		}
		if (nofButton.equals(FOLLOW)) {
			mBtnPhotoFollowUser.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_choose, 0, 0, 0);
		}
		if (nofButton.equals(OFF)) {
			mBtnPhotoOff.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_choose, 0, 0, 0);
		}
		this.mNofButton = nofButton;
	}
	
	/**
	 * Set state of Button comment
	 * 
	 * @param nofComment
	 */
	public void setNofComment(String nofComment) {
		mBtnCommentAllUser.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_not_choose, 0, 0, 0);
		mBtnCommentFollowUser.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_not_choose, 0, 0, 0);
		mBtnCommentOff.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_not_choose, 0, 0, 0);
		if (nofComment.equals(ALL)) {
			mBtnCommentAllUser.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_choose, 0, 0, 0);
		}
		if (nofComment.equals(FOLLOW)) {
			mBtnCommentFollowUser.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_choose, 0, 0, 0);
		}
		if (nofComment.equals(OFF)) {
			mBtnCommentOff.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_choose, 0, 0, 0);
		}
		this.mNofComment = nofComment;
	}
	
	/**
	 * Set state of Button lost title
	 * 
	 * @param lostTitle
	 */
	public void setLoseTitle(String lostTitle) {
		mBtnTitleLosePush.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_not_choose, 0, 0, 0);
		mBtnTitleLoseOff.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_not_choose, 0, 0, 0);
		if (lostTitle.equals(Constant.PUBLIC)) {
			mBtnTitleLosePush.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_choose, 0, 0, 0);
		}
		if (lostTitle.equals(Constant.NOT_PUBLIC)) {
			mBtnTitleLoseOff.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_choose, 0, 0, 0);
		}
	}
	
	/**
	 * Set state of Button contact
	 * 
	 * @param nofContact
	 */
	public void setNofContact(String nofContact) {
		mBtnContactAllUser.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_not_choose, 0, 0, 0);
		mBtnContactOff.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_not_choose, 0, 0, 0);
		if (nofContact.equals(Constant.PUBLIC)) {
			mBtnContactAllUser.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_choose, 0, 0, 0);
		}
		if (nofContact.equals(Constant.NOT_PUBLIC)) {
			mBtnContactOff.setCompoundDrawablesWithIntrinsicBounds(R.drawable.u05_choose, 0, 0, 0);
		}
		this.mNofContact = nofContact;
	}

	
}
